package com.thinkcar.core.exception;

import com.thinkcar.core.enums.PublicExceptionEnum;
import lombok.Data;

/**
 * @author: jiangyong
 * @date: 2025/10/18
 * @description: GlobalExceptionHandler
 */
@Data
public class Result<T> {

    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应结果
     */
    private T data;

    public Result(T data) {
        this.code = PublicExceptionEnum.SUCCESS.getCode();
        this.msg = PublicExceptionEnum.SUCCESS.getMsg();
        this.data = data;
    }

    public Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    /**
     * 成功
     *
     * @return ResultResponse
     */
    public static <T> Result<T> success() {
        return success(null);
    }

    /**
     * 成功
     *
     * @param data data
     * @return ResultResponse
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }

    /**
     * 失败
     *
     * @param error error
     * @return ResultResponse
     */
    public static <T> Result<T> failed(PublicExceptionEnum error) {
        return new Result<>(error.getCode(), error.getMsg());
    }

    /**
     * 失败
     *
     * @param code code
     * @param msg  msg
     * @return ResultResponse
     */
    public static <T> Result<T> failed(String code, String msg) {
        return new Result<>(code, msg);
    }

}
